package ssf.app.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ssf.app.form.EmployeeForm;
import ssf.core.common.PagingList;
import ssf.core.Constants;

public class ExcelAction extends BaseAction {

		@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		PagingList employeeList = getServMgr().getEmployeeService().getAllEmployees(request);
		List list = employeeList.getList();
		EmployeeForm employeeform = (EmployeeForm)form;
		//System.out.println(list);
		//建工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("招聘信息");
		//给工作表前9列定义列宽
		sheet.setColumnWidth((short)0,(short)4000);
		sheet.setColumnWidth((short)1,(short)4000);
		sheet.setColumnWidth((short)2,(short)4000);
		sheet.setColumnWidth((short)3,(short)4000);
		sheet.setColumnWidth((short)4,(short)4000);
		sheet.setColumnWidth((short)5,(short)4000);
		sheet.setColumnWidth((short)6,(short)4000);
		sheet.setColumnWidth((short)7,(short)4000);
		sheet.setColumnWidth((short)8,(short)4000);
		//在表中建行
		HSSFRow row = sheet.createRow((short)0);
		HSSFCell[] cell = new HSSFCell[9];
		for (int i = 0; i < 9; i++) {
		cell[i] = row.createCell(i);
		//将单元格定义成UTF_16编码，这样才能使输出数据不会乱码
		cell[i].setCellType(HSSFCell.CELL_TYPE_STRING);

		}
		//写单元格标题
		cell[0].setCellValue("编号");
		cell[1].setCellValue("用户名");
		cell[2].setCellValue("真实姓名");
		cell[3].setCellValue("性别");
		cell[4].setCellValue("系别");
		cell[5].setCellValue("专业");
		cell[6].setCellValue("班级");
		cell[7].setCellValue("手机");
		cell[8].setCellValue("其他信息");
		//查询数据库，取得数据列表的List实例
		//List list =null;
		//CreateList cl=new CreateList();
		try {
		//list = employeeList.createList();
		} catch (Exception e) {
		e.printStackTrace(); 
		}
		//从List中取出数据放入工作表中
		if (list != null && list.size() > 0) {
		
			for (int i = 0; i < list.size(); i++) {
				
				Map query = getServMgr().getEmployeeService().showDetail(String.valueOf(i));
				EmployeeForm ef=(EmployeeForm) query.get(i);
				
		//上面已经创建了一行
		HSSFRow datarow = sheet.createRow(i + 1);
		HSSFCell[] data = new HSSFCell[9];
		for (int j = 0; j < 9; j++) {
		data[j] = datarow.createCell(j);
		//将单元格定义成UTF_16编码，这样才能使输出数据不会乱码
		data[j].setCellType(HSSFCell.CELL_TYPE_STRING);
		}
		data[0].setCellValue(ef.getEmpId());
		/*data[1].setCellValue(query.getId());
		data[2].setCellValue(query.getName());
		data[3].setCellValue(query.getSex());
		data[4].setCellValue(query.getDept());
		data[5].setCellValue(query.getMajor());
		data[6].setCellValue(query.getClasses());
		data[7].setCellValue(query.getTel());
		data[8].setCellValue(query.getOther());*/
		
		}
		}
		

		//也可输出成xls文件
		File file = new File("d:\\workbook.xls");
		try {
		FileOutputStream fileOut = new FileOutputStream(file);
		wb.write(fileOut);
		fileOut.close();
		}catch (IOException e) {
		e.printStackTrace(); 
		}
		
		setResult(request, "ok");
        addMessage(request, "chenggong");
        addRedirURL(request, "返回", "@back");
        return mapping.findForward(EXECUTE_RESULT);
		}

		
}
